const { Department } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
const Sequelize = require('sequelize')

// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { departmentName, departmentDescription, manage, contact, departmentID, searchType, pageNum = 1, // 默认页码为1
        pageSize = 5 } = req.body
    try {
        switch (operation) {
            case 'create':
                // // 根据客户端传递过来的 用户名 查询 数据库 中 是否 存在这用户名
                const model = await Department.findOne({ where: { departmentName }, attributes: { exclude: ['createdAt', 'updatedAt'] } })
                // // 判断
                if (model) {
                    res.status(400).send({
                        data: null,
                        meta: {
                            msg: "部门已经存在！",
                            status: 400
                        }
                    })
                    return
                }
                const state = {
                    DepartmentName: departmentName,
                    DepartmentDescription: departmentDescription,
                    Manage: manage,
                    Contact: contact
                }
                const newDepData = await Department.create(state)
                const data = await Department.findOne({
                    where: {
                        DepartmentName: state.DepartmentName
                    }
                })
                res.status(200).send({
                    data,
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                // 实现分页
                let whereCondition = {}; // 默认查询条件为空对象
                if (searchType === "exact" && departmentName) {
                    // 执行精确搜索
                    whereCondition = { DepartmentName: { [Sequelize.Op.eq]: departmentName } };
                } else if (searchType === "fuzzy" && departmentName) {
                    // 执行模糊搜索
                    whereCondition = { DepartmentName: { [Sequelize.Op.like]: `%${departmentName}%` } };
                }

                const total = await Department.count({ where: whereCondition }); // 根据查询条件获取数据总数

                const offset = (pageNum - 1) * pageSize; // 计算偏移量
                const depData = await Department.findAll({
                    where: whereCondition,
                    limit: Number(pageSize),
                    offset: Number(offset),
                })

                console.log(depData);
                res.status(200).send({
                    data: {
                        depData,
                        total,
                        pageSize: Number(pageSize),
                        pageNum: Number(pageNum),
                    },
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'update':
                const stateData = {
                    DepartmentName: departmentName,
                    DepartmentDescription: departmentDescription,
                    Manage: manage,
                    Contact: contact
                }
                const upDepData = await Department.update(stateData, { where: { departmentID: departmentID } },)
                console.log(upDepData);
                res.status(200).send({
                    meta: {
                        msg: "编辑成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'delete':
                const delData = await Department.destroy({ where: { departmentID: departmentID } })
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break
            case 'findAllDepartmentList':
                const departments = await Department.findAll({
                    attributes: ['DepartmentName'] // 只选择用户名字段
                });
                const departmentList = departments.map(dep => dep.DepartmentName);
                // 打印sql语句
                res.status(200).send({
                    departmentList,
                    meta: {
                        msg: "获取成功！",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'findDepartmentAndID':
                const departmentAndID = await Department.findAll({
                    attributes: ['DepartmentName', "DepartmentID"] // 只选择用户名字段
                });
                // 打印sql语句
                res.status(200).send({
                    departmentAndID,
                    meta: {
                        msg: "获取成功！",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'readAll':
                const tableData = await Department.findAll()
                res.status(200).send({
                    data: {
                        tableData,
                    },
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
            default:
                break;
        }

    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}